let list=JSON.parse(sessionStorage.getItem("key"));

$(function () {
    addGoodsCart();
});


function refreshAmount() {
    let amount = 0;
    list.forEach(item=>{
        amount += parseInt(item.subtotal);
    });
    $("#big_sum_amount").html(amount);
}

function addGoodsCart() {

    //let list=JSON.parse(sessionStorage.getItem("key"));


    //list.forEach(item=>console.log(item.goodsNum));

    //业务内容清空
    $("#table_goods").html("");

    let goodsListHtml = "";
    //获取模板
    let goodsTemplate = $("#template_goods").html();

    list.forEach(item=>{
        let goodsHtml = goodsTemplate
            .replaceAll("{{goodsUrl}}", item.imgUrl)
            .replaceAll("{{goodsName}}", item.remark)
            .replaceAll("{{goodsPrice}}", item.price)
            .replaceAll("{{id}}", item.goodsId)
            .replaceAll("{{subtotal}}", item.subtotal)
            .replaceAll("{{number}}", item.goodsNum);
        //拼接HTML
        goodsListHtml += goodsHtml;
    });
    //填充html到指定位置
    $("#table_goods").html(goodsListHtml);

    refreshAmount();
}



//删除商品
function delGoods(id) {
    console.log("delGoods:" + id);
    //获取要删除数据
    //数组删除指定数据
    let tempGoods;
    for (let i = 0; i < list.length; i++) {
        tempGoods = list[i];
        if (tempGoods.goodsId == id) {
            console.log(tempGoods);
            //数据删除
            list.splice(i, 1);
            sessionStorage.setItem("key",JSON.stringify(list));
            break;
        }
    }
    //重新渲染列表
    addGoodsCart();
}


function delGoodsList() {
    console.log("delGoodsList");
    //获取到要删除的商品数据
    //获取所有标记选中的checkbox
    let selectedCheckBoxList = $("input:checked");
    //获取每个checkbox中value
    let selectedIds = [];
    let id;
    for (let i = 0; i < selectedCheckBoxList.length; i++) {
        id = parseInt(selectedCheckBoxList[i].value);
        selectedIds.push(id);
    }

    //数组删除指定商品
    let tempGoods;
    for (let i = list.length - 1; i >= 0; i--) {
        tempGoods = list[i];


        if (selectedIds.toString().indexOf(tempGoods.goodsId) >= 0) {
            console.log(tempGoods.goodsId);
            //是 删除数据
            list.splice(i, 1);
            continue;
        }
    }
    sessionStorage.setItem("key",JSON.stringify(list));
    //重新渲染列表
    addGoodsCart();
}

function checkboxAll(allCheckBox) {
    console.log("checkboxAll");
    //获取当前全选checkbox 状态
    let status = allCheckBox.checked;
    //获取所有checkbox
    $("input[type='checkbox']").each(function (index, element) {
        //修改所有商品checkbox状态
        element.checked = status;
    })
}


function cancelCheckAll(goodsCheckbox) {
    //判断当前商品选中状态
    console.log(goodsCheckbox.checked);

    if (!goodsCheckbox.checked) {
        //如果是未选中，取消全选
        $(".checkbox-all").each(function (index, element) {
            element.checked = false;
        });
    }
}


//递增
function increaseNumber(tag,id) {

    changeNumber(id,tag+1);
}

function reduceNumber(tag, id) {
    changeNumber(id,tag-1);
}

//修改数量
function changeNumber(id, newNumber) {

    if (newNumber <= 0) {
        window.alert("商品数量不能少于等于0！");
        return;
    }

    //修改数组中数据

    list.forEach(goods => {

        if (goods.goodsId == id) {
            //获取当前商品,修改商品数量，小计，累加总价
            goods.goodsNum = newNumber;

            goods.subtotal =Math.floor(parseInt(goods.price) * parseInt(goods.goodsNum) * 100) / 100
        }
    });
    sessionStorage.setItem("key",JSON.stringify(list));
    //重新渲染列表
    addGoodsCart();
}


//清空购物车
function delAll() {

    list = [];
    sessionStorage.setItem("key",JSON.stringify(list));
    //重新渲染列表
    addGoodsCart();
    window.alert("清空购物车成功！")
}
